package org.zkdn.modules.wlpt.basicinfo.controller;

import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.zkdn.modules.wlpt.basicinfo.dto.CrossDTO;
import org.zkdn.modules.wlpt.basicinfo.entity.TCrossing;
import org.zkdn.modules.wlpt.basicinfo.service.ITCrossingService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.zkdn.modules.wlpt.basicinfo.vo.CrossVO;

/**
 * @Description: t_crossing
 * @Author: jeecg-boot
 * @Date:   2025-06-18
 * @Version: V1.0
 */
@Api(tags="t_crossing")
@RestController
@RequestMapping("/basicinfo/roadSurface/crossing")
@Slf4j
public class TCrossingController extends JeecgController<TCrossing, ITCrossingService> {
	@Autowired
	private ITCrossingService crossingService;

	 @RequestMapping(value = "/listAll", method = RequestMethod.GET)
	 public Result<List<CrossVO>> listAll(TCrossing cross) {
		 Result<List<CrossVO>> result = new Result<>();
		 QueryWrapper<TCrossing> queryWrapper = new QueryWrapper<>();
		 //查询未删除状态
		 queryWrapper.eq("crossAlias.del_flag", 0);
		 Optional.ofNullable(cross.getName()).ifPresent(name -> queryWrapper.like("crossAlias.name", name));
		 Optional.ofNullable(cross.getRoadId()).ifPresent(roadId -> {
			 queryWrapper.eq("refRoadAlias.road_id", roadId);
		 });
		 List<CrossVO> list = crossingService.queryList(queryWrapper);
		 if(list==null||list.size()<=0) {
			 result.error500("未找到路口信息");
		 }else {
			 result.setResult(list);
			 result.setSuccess(true);
		 }
		 return result;
	 }

	/**
	 * 分页列表查询
	 *
	 * @param cross
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "t_crossing-分页列表查询")
	@ApiOperation(value="t_crossing-分页列表查询", notes="t_crossing-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CrossVO>> queryPageList(TCrossing cross,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("create_time");
		//查询未删除状态
		queryWrapper.eq("crossAlias.del_flag", 0);

		Optional.ofNullable(cross.getCode()).ifPresent(deviceId -> {
			queryWrapper.like("crossAlias.code", deviceId);
		});
		Optional.ofNullable(cross.getName()).ifPresent(serialNumber -> {
			queryWrapper.like("crossAlias.name", serialNumber);
		});
		Optional.ofNullable(cross.getCrossForm()).ifPresent(deviceType -> {
			queryWrapper.eq("crossAlias.cross_form", deviceType);
		});
		Optional.ofNullable(cross.getRoadId()).ifPresent(manufacturerId -> {
			queryWrapper.eq("refRoadAlias.road_id", manufacturerId);
		});
		Optional.ofNullable(cross.getZoneId()).ifPresent(poleId -> {
			queryWrapper.eq("crossAlias.zone_id", poleId);
		});
		Optional.ofNullable(cross.getLevel()).ifPresent(crossId -> {
			queryWrapper.eq("crossAlias.level", crossId);
		});
		Optional.ofNullable(cross.getCrossTag()).ifPresent(crossTag -> {
			List<String> crossTaglist = Arrays.stream(crossTag.split(","))
					.filter(s -> !s.isEmpty()) // 过滤空字符串
					.collect(Collectors.toList());
			queryWrapper.in("refTagAlias.dict_item_value", crossTaglist);
		});
		IPage<CrossVO> page = new Page<>(pageNo, pageSize);
		IPage<CrossVO> pageList = crossingService.queryPageList(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param crossDTO
	 * @return
	 */
	@AutoLog(value = "t_crossing-添加")
	@ApiOperation(value="t_crossing-添加", notes="t_crossing-添加")
	//@RequiresPermissions("org.zkdn.modules.wlpt:t_crossing:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CrossDTO crossDTO) {
		return crossingService.saveByValidate(crossDTO);
	}

	/**
	 *  编辑
	 *
	 * @param crossDTO
	 * @return
	 */
	@AutoLog(value = "t_crossing-编辑")
	@ApiOperation(value="t_crossing-编辑", notes="t_crossing-编辑")
	//@RequiresPermissions("org.zkdn.modules.wlpt:t_crossing:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CrossDTO crossDTO) {
		return crossingService.updateByValidate(crossDTO);
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "t_crossing-通过id删除")
	@ApiOperation(value="t_crossing-通过id删除", notes="t_crossing-通过id删除")
	//@RequiresPermissions("org.zkdn.modules.wlpt:t_crossing:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		return crossingService.deleteById(id);
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "t_crossing-批量删除")
	@ApiOperation(value="t_crossing-批量删除", notes="t_crossing-批量删除")
	//@RequiresPermissions("org.zkdn.modules.wlpt:t_crossing:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		return crossingService.deleteByIds(ids);
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "t_crossing-通过id查询")
	@ApiOperation(value="t_crossing-通过id查询", notes="t_crossing-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<TCrossing> queryById(@RequestParam(name="id",required=true) String id) {
		TCrossing tCrossing = crossingService.getById(id);
		if(tCrossing==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(tCrossing);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param tCrossing
    */
    //@RequiresPermissions("org.zkdn.modules.wlpt:t_crossing:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TCrossing tCrossing) {
        return super.exportXls(request, tCrossing, TCrossing.class, "t_crossing");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("t_crossing:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TCrossing.class);
    }

}
